package cn.tansanqinger.dao.sql.announcement;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.IAnnouncementDisposeSqlDao;
import cn.tansanqinger.dao.sql.IAnnouncementSqlDao;
import cn.tansanqinger.pojo.Announcement;
import cn.tansanqinger.pojo.Announcementdispose;
import cn.tansanqinger.pojo.User;

@Repository
public class AnnouncementSqlDaoImpl {

	@Autowired
	private IAnnouncementSqlDao dao;
	@Autowired
	private IAnnouncementDisposeSqlDao disdao;
	
	/**
	 * 查询公告信息
	 * @param user
	 * @return
	 */
	public List<Announcement> selectAnnouncements(User user, int page){
		QueryWrapper<Announcementdispose> queryWrapper = new QueryWrapper<Announcementdispose>();
		queryWrapper.lambda().eq(Announcementdispose::getUid, user.getId()).eq(Announcementdispose::getDispose, 1);
		List<Announcementdispose> announcementdisposes = disdao.selectList(queryWrapper);
		QueryWrapper<Announcement> queryWrapper2 = new QueryWrapper<Announcement>();
		queryWrapper2.lambda().ne(Announcement::getAudiences, user.getJurisdiction());
		for (int i = 0; i < announcementdisposes.size(); i++) {
			queryWrapper2.lambda().ne(Announcement::getAid, announcementdisposes.get(i).getAid());
		}
		Page<Announcement> pg = new Page<Announcement>(page,10);
		IPage<Announcement> iPage = dao.selectPage(pg, queryWrapper2);
		List<Announcement> announcements = iPage.getRecords();
		for (int i = 0; i < announcements.size(); i++) {
			queryWrapper = new QueryWrapper<Announcementdispose>();
			queryWrapper.lambda().eq(Announcementdispose::getAid, announcements.get(i).getAid()).eq(Announcementdispose::getUid, user.getId());
			if(null==disdao.selectOne(queryWrapper)) {
				Announcementdispose announcementdispose = new Announcementdispose();
				announcementdispose.setAid(announcements.get(i).getAid());
				announcementdispose.setDispose(0);
				announcementdispose.setUid(user.getId());
				disdao.insert(announcementdispose);
			}
		}
		return announcements;
	}
	
	/**
	 * 删除数据
	 * @param uid
	 * @param aid
	 * @return
	 */
	public int delete(int uid, int aid) {
		QueryWrapper<Announcementdispose> queryWrapper = new QueryWrapper<Announcementdispose>();
		queryWrapper.lambda().eq(Announcementdispose::getAid, aid).eq(Announcementdispose::getUid, uid);
		Announcementdispose announcementdispose = disdao.selectOne(queryWrapper);
		announcementdispose.setDispose(1);
		return disdao.updateById(announcementdispose);
	}
	
	/**
	 * 查询单一数据
	 * @param aid
	 * @return
	 */
	public Announcement selectAnnouncementOne(int aid) {
		return dao.selectById(aid);
	}
	
	/**
	 * 查询还有多少数据未读
	 * @param user
	 * @return
	 */
	public int selectAnnouncementNumber(User user) {
		int number = 0;
		QueryWrapper<Announcement> queryWrapper = new QueryWrapper<Announcement>();
		queryWrapper.lambda().ne(Announcement::getAudiences, user.getJurisdiction());
		List<Announcement> announcementes = dao.selectList(queryWrapper);
		for (int i = 0; i < announcementes.size(); i++) {
			QueryWrapper<Announcementdispose> queryWrapper2 = new QueryWrapper<Announcementdispose>();
			queryWrapper2.lambda().eq(Announcementdispose::getAid, announcementes.get(i).getAid()).eq(Announcementdispose::getUid, user.getId());
			if(null==disdao.selectOne(queryWrapper2)) {
				number ++;
			}
		}
		return number;
	}
	
	public int announcementNumber(User user) {
		QueryWrapper<Announcementdispose> queryWrapper = new QueryWrapper<Announcementdispose>();
		queryWrapper.lambda().eq(Announcementdispose::getUid, user.getId()).eq(Announcementdispose::getDispose, 1);
		List<Announcementdispose> announcementdisposes = disdao.selectList(queryWrapper);
		QueryWrapper<Announcement> queryWrapper2 = new QueryWrapper<Announcement>();
		queryWrapper2.lambda().ne(Announcement::getAudiences, user.getJurisdiction());
		for (int i = 0; i < announcementdisposes.size(); i++) {
			queryWrapper2.lambda().ne(Announcement::getAid, announcementdisposes.get(i).getAid());
		}
		return dao.selectCount(queryWrapper2);
	}
}
